﻿using System;
using System.Data;
using System.Globalization;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Media;

namespace MDSAdministration
{
    public class ExcelUploadColourHighlighterHeaderConverter : IMultiValueConverter
    {
        #region Implementation of IMultiValueConverter

        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {

            if (values[0] is DataGridColumnHeader && values[1] is DataGrid && values[2] is CompareModel)
            {
                var cell = (DataGridColumnHeader)values[0];
                cell.Background = null;
                cell.ToolTip = null;

                var dg = (DataGrid) values[1];
                if (dg.ItemsSource != null && cell.Column != null)
                {
                    var tableName = ((DataView) dg.ItemsSource).Table.TableName;
                    var model = (CompareModel) values[2];

                    if (model.Entities.ContainsKey(CompareEntity.getEntityKey(tableName)))
                    {
                        if (model.Entities[CompareEntity.getEntityKey(tableName)].Attributes.ContainsKey(
                            CompareAttribute.getAttributeKey(cell.Column.Header.ToString(), Constants.MEMBER_TYPE_LEAF)) ==
                            false)
                        {
                            cell.Background = Brushes.Yellow;
                            cell.ToolTip = "Column doesn't exist in Entity.";
                        }
                        else
                        {
                            var a = model.Entities[CompareEntity.getEntityKey(tableName)].Attributes[
                                CompareAttribute.getAttributeKey(cell.Column.Header.ToString(),
                                    Constants.MEMBER_TYPE_LEAF)];
                            if (a.IsSystem == true && a.IsCode == false && a.IsName == false)
                            {
                                cell.Background = Brushes.Orange;
                            }
                        }
                    }
                }

            }
            return null;
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            throw new System.NotImplementedException();
        }

        #endregion
    }
}